package TestPaperSystem.service.impl;

import TestPaperSystem.entity.ExamRepo;
import TestPaperSystem.entity.Qu;
import TestPaperSystem.entity.QuRepo;
import TestPaperSystem.mapper.QuRepoMapper;
import TestPaperSystem.service.QuRepoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xwj
 * @since 2023-03-25
 */
@Service
public class QuRepoServiceImpl extends ServiceImpl<QuRepoMapper, QuRepo> implements QuRepoService {


    @Resource
    QuRepoMapper quRepoMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    /*
    对于QuRepo表的修改每次可能涉及insert、delete
     */
    public Boolean saveData(List<String> repoIds, Qu qu) {
        List<QuRepo> lists = quRepoMapper.selectList(new QueryWrapper<QuRepo>().lambda().eq(QuRepo::getQuId,qu.getId()));
        List<QuRepo> list_insert = new ArrayList<>();
        List<Integer> list_delete = new ArrayList<>();

        Boolean flag = false;
        //当前数据库查询出来的qu_Repo中的repoId与传入的repoIds比较，若数据库中没有则加入数据库中
        for(String e : repoIds){
            flag = false;
            for(QuRepo qu_repo : lists)
                if(qu_repo.getRepoId().equals(e)){
                    flag = true;
                    break;
                }

            if(!flag){
                QuRepo quRepo = new QuRepo();
                quRepo.setSort(0);
                quRepo.setRepoId(e);
                quRepo.setQuType(qu.getQuType());
                quRepo.setQuId(qu.getId());
                list_insert.add(quRepo);
            }
        }

        //当前数据库查询出来的qu_Repo中的repoId与传入的repoIds比较，若传入的数据没有则从数据库中删除
        for(QuRepo e : lists){
            if(!repoIds.contains(e.getRepoId())){
                list_delete.add(e.getId());
            }
        }
        if(list_delete.size() == 0)
            return this.saveBatch(list_insert);
        else
            return this.saveBatch(list_insert)
                & (quRepoMapper.deleteQuRepoListById(list_delete) > 0);
    }

    @Override
    public List<String> getQuListFromRepo(String repoId, List<String> excludes, int quType, int size) {

        QueryWrapper<QuRepo> quRepoQueryWrapper = new QueryWrapper<>();
        quRepoQueryWrapper.lambda().eq(QuRepo::getRepoId, repoId)
                .notIn(!excludes.isEmpty(),QuRepo::getQuId, excludes)
                .last("limit " + size);
        if(quType > 3){
            quRepoQueryWrapper.lambda().ge(QuRepo::getQuType,quType);
        }else{
            quRepoQueryWrapper.lambda().eq(QuRepo::getQuType,quType);
        }

        List<String> list = quRepoMapper.selectList(quRepoQueryWrapper).stream().map(QuRepo::getQuId).collect(Collectors.toList());
        return list;
    }


}
